<?php

namespace app\customer\controller\customer\rongcui;

use app\rongcui\model\CustomerNatureModel;
use app\rongcui\model\RongcuiBuyerModel;
use app\rongcui\model\RongcuiIndustryCateModel;

class BuyerController extends \app\rongcui\controller\BuyerController
{
    function index() {
        $where = [];
        $cate_id = input('category','-1');
        if($cate_id != -1) {
            $where['ctype.cate_id'] = $cate_id;
        }
        if (input('source')) {
            $where['buyer.source'] = input('source');
        }
        if (input('nature')) {
            $where['buyer.nature'] = input('nature');
        }
        if (input('is_done')) {
            $where['buyer.is_done'] = input('is_done', 0, 'intval') - 1;
        }
        if (input('start_time')) {
            $where['buyer.createtime'] = ['gt', strtotime(input('start_time'))];
        }
        if (input('end_time')) {
            $endtime = strtotime(input('end_time'));
            $datetime = strtotime(date('Y-m-d', $endtime));
            if ($endtime == $datetime) {
                $endtime += 86399;
            }
            if (isset($where['buyer.createtime'])) {
                $where['buyer.createtime'] = ['BETWEEN', [strtotime(input('start_time')), $endtime]];
            } else {
                $where['buyer.createtime'] = ['lt', $endtime];
            }
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname'] = ['like', "%$keyword%"];
        }

        $buyerModel = new RongcuiBuyerModel();
        $lists = $buyerModel
            ->alias('buyer')
            ->join('__RONGCUI_BUYER_USER__ buser', 'buyer.id = buser.pid and buser.level = 1')
            ->join('__USER__ user', 'buser.uid = user.id')
            ->join('__RONGCUI_INDUSTRY_TYPE__ ctype', 'buyer.id = ctype.pid')
            ->join('__CUSTOMER_NATURE__ nature', 'buyer.nature = nature.id','left')
            ->group('buyer.id')
            ->where('char_length(buyer.name) <= 8')
            ->where($where)
            ->field([
                'buyer.*',
                'nature.name nname',
                'user.user_nickname'
            ])
            ->order('buyer.createtime desc,buyer.id desc')
            ->paginate();

        $rongcuiIndustryCateModel = new RongcuiIndustryCateModel();
        $cate = $rongcuiIndustryCateModel->where([
            'status' => 1,
            'is_del' => 0
        ])->order('parent_id,list_order,id')->select();

        $customerNatureModel = new CustomerNatureModel();
        $source = $customerNatureModel->where([
            'type' => 2,
            'is_del' => 0
        ])->field('id,name')->select();

        $nature = $customerNatureModel->where([
            'type' => 8,
            'is_del' => 0
        ])->field('id,name')->select();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('lists', $lists);
        $this->assign('source', $source);
        $this->assign('nature', $nature);
        $this->assign('where_r', $where_r);
        $this->assign('page', $page);
        $this->assign('cate', $cate);
        return $this->fetch();
    }
}